//
//  NvAssetManager.m
//  NvSellerShow
//
//  Created by Meicam on 2017/2/18.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "NvAssetManager.h"

@implementation NvAssetManager

- (instancetype)init {
    if(self = [super init]){
        
    }
    return self;
}

//lazy property load
- (NSFileManager *)fileManager {
    if(_fileManager == nil)
        _fileManager = [NSFileManager defaultManager];
    return _fileManager;
}

- (NvAssetModel *)getAssetsModelWithPath:(NvAssetType)assetType {
    NvAssetModel *model;
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"NvSellerResource" ofType:@"bundle"];
    NSString *assetPath = [bundlePath stringByAppendingPathComponent:@"BundleData"];
    NSString *reservedDir;
    if(assetType == NvAssetTheme) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_THEME_DIR];
    } else if(assetType == NvAssetAnimatedSticker) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_STICKER_DIR];
    } else if(assetType == NvAssetCaptionStyle) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_CAPTION_STYLE_DIR];
    }
    if([self.fileManager fileExistsAtPath:reservedDir]){
        NSArray *assetDirArray = nil;
        NSError *error = nil;
        NSString * assetPath;

        assetDirArray = [self.fileManager contentsOfDirectoryAtPath:reservedDir error:&error];
        NSLog(@"%@", assetDirArray);
        for(NSString * assetDirName in assetDirArray){
            assetPath = [reservedDir stringByAppendingPathComponent:assetDirName];
            model = [self parseAssetModelWithPath:assetPath andAssetId:assetDirName andType:assetType];
        }
    }
    return model;
}


- (NSArray *)getAssetsWithAssetType:(NvAssetType)assetType{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"NvSellerResource" ofType:@"bundle"];
    NSString *assetPath = [bundlePath stringByAppendingPathComponent:@"BundleData"];
    NSMutableArray * assetModelsArray = [NSMutableArray array];
    NSString *reservedDir;
    if(assetType == NvAssetTheme) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_THEME_DIR];
    } else if(assetType == NvAssetAnimatedSticker) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_STICKER_DIR];
    } else if(assetType == NvAssetCaptionStyle) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_CAPTION_STYLE_DIR];
    } else if (assetType == NvAssetVideoTransition) {
        reservedDir = [assetPath stringByAppendingPathComponent:NS_BUNDLE_RESERVED_STICKER_DIR];
    }
    
    if([self.fileManager fileExistsAtPath:reservedDir]){
        NSLog(@"exist requireDir: %@", reservedDir);
        NSArray *assetDirArray = nil;
        NSError *error = nil;
        NSString * assetPath;
        NvAssetModel *model;
        assetDirArray = [self.fileManager contentsOfDirectoryAtPath:reservedDir error:&error];
        NSLog(@"%@", assetDirArray);
        for(NSString * assetDirName in assetDirArray){
            assetPath = [reservedDir stringByAppendingPathComponent:assetDirName];
            model = [self parseAssetModelWithPath:assetPath andAssetId:assetDirName andType:assetType];
            if(model != nil)
                [assetModelsArray addObject:model];
        }
        
    }

    return assetModelsArray;
}



//////////////////////

- (NvAssetModel *) parseAssetModelWithPath:(NSString *)path andAssetId:(NSString *)assetId andType:(NvAssetType)type {
    NvAssetModel *model = nil;
    NSString * jsonInfoPath = [path stringByAppendingString:@"/info.json"];
    if([self.fileManager fileExistsAtPath:jsonInfoPath]){
        //parse info.json in assetPath
        NSData *jdata = [[NSData alloc]initWithContentsOfFile:jsonInfoPath];
        NSError *error;
        NSDictionary *jsonObj = [NSJSONSerialization JSONObjectWithData:jdata options:0 error:&error];
        if(error || jsonObj == nil){
            return nil;
        }
        NSString *uuid = [jsonObj objectForKey:@"uuid"];
        if(![uuid isEqualToString:assetId])
            return nil;
        model = [[NvAssetModel alloc]initWithPath:path andId:assetId andType:type];
        NSString *packageName;
        if(type == NvAssetTheme){
            packageName = [NSString stringWithFormat:@"%@.theme",assetId];
        }
        //other assetType
        else if(type == NvAssetAnimatedSticker){
            packageName = [NSString stringWithFormat:@"%@.animatedsticker",assetId];
        }
        else if(type == NvAssetCaptionStyle) {
            packageName = [NSString stringWithFormat:@"%@.captionstyle",assetId];
        }
        model.assetPackagePath = [path stringByAppendingPathComponent:packageName];
        model.displayName = [jsonObj objectForKey:@"displayname"];
        model.imageCoverPath = [path stringByAppendingPathComponent:[jsonObj objectForKey:@"cover"]];
        model.version = [[jsonObj objectForKey:@"version"] integerValue];
        model.licensePath = [path stringByAppendingPathComponent:[jsonObj objectForKey:@"licensePath"]];
        
        if([jsonObj objectForKey:@"preview"])
            model.previewVideoPath = [path stringByAppendingPathComponent:[jsonObj objectForKey:@"preview"]];
        
    }
    
    return model;
}

- (NSString *)stringByRemovingControlCharacters: (NSString *)inputString
{
    NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];
    NSRange range = [inputString rangeOfCharacterFromSet:controlChars];
    if (range.location != NSNotFound) {
        NSMutableString *mutable = [NSMutableString stringWithString:inputString];
        while (range.location != NSNotFound) {
            [mutable deleteCharactersInRange:range];
            range = [mutable rangeOfCharacterFromSet:controlChars];
        }
        return mutable;
    }
    return inputString;
}

@end
